------------------------------------------------------------------------------------------

                             Instruction of Codes for the Paper 
'The Tip of the Iceberg: A Quantitative Framework for Estimating Trade Costs' by Irarrazabal, Moxnes and Opromolla (2014)

------------------------------------------------------------------------------------------



Contents:



******************************************* 
***Data dictionary***
*******************************************

All the data used in the STATA programs are collected in the .../data/ folder, which includes:

moxnes_lopenr_2004.dta              contains the firm product destination data from Statistics Norway. 
                                    It contain the following variables (names in Norwegian as original data):
                                    1) foretak: firm id, 
                                    2) abland: country of origin, 
                                    3) obland: destination country
                                    4) varnr: HS8 product classification, 
                                    5) verdi: value, 
                                    6) mengde: alternative unit (used if present), 
                                    7) year



penn62_iso2_dist_small.dta          contains macro variables from Penn Table 6.2 variables: 
                                    1) iso code, 
                                    2) year, 
                                    3) dist: distance, 
                                    4) pop: population, 
                                    5) cgdp: real GDP per capita.


	
Two intermeidiate datasets are also provided in .../data_matlab/:
res_2004_20thresh_Restat7.mat
res_2004_40thresh_Restat7.mat
They are generated by running estimation_short_restat.m with 'thresh = 20' and 'thresh = 40' in line 10, in folder .../matlab_code/.


Other intermediate datasets will be generated by the above raw data and saved in the .../tmp/ and .../data_matlab/



In order to reproduce the figures and tables in the paper, run the do-files in the following order. 

*********************************************
***Data preparation ***
*********************************************

1. ImportMergeData.do               It imports and merge moxnes_lopenr_2004.dta and penn62_iso2_dist_small.dta  ;
                                    This file generates the following intermediate data files:
	                                a. tmp_tc.dta

2. export_to_matlab.do 		    It imports and merge moxnes_lopenr_2004.dta and penn62_iso2_dist_small.dta  ;
                                    The file prepares data and export it as text file to be used in Matlab. 
                                    This will generate the following intermediate data files in the .../tmp/:
                                    a. tmp.dta
                                    b. tmp2.dta

                                    Changing the parameter 'thresh' between 20 and 40 in line 39 generates different datasets.
                                    It exports the following data in .../data_matlab/ used in Matlab files.
                                    a. data_dist_gdp_20thresh_2004.raw and data_dist_gdp_40thresh_2004.raw
                                    b. weights_20thresh_2004.dta and weights_40thresh_2004.dta (and .raw files)
                                    c. weight_value_20thresh_2004.dta and weight_value_40thresh_2004.dta (and .raw files)
                                    d. weight_value_SE_20thresh_2004.dta and weight_value_SE_40thresh_2004.dta (and .raw files)
                                    e. weight_qty_20thresh_2004.dta and weight_qty_40thresh_2004.dta (and .raw files)
                                    f. weight_qty_SE_20thresh_2004.dta and weight_qty_SE_40thresh_2004.dta (and .raw files)
                                    g. data_qty_20thresh_2004.raw and data_qty_40thresh_2004.raw
                                    h. data_price_20thresh_2004.raw and data_price_40thresh_2004.raw
                                    i. data_2004_count_20thresh.raw and data_2004_count_40thresh.raw



*********************************************
***Creates Tables***
*********************************************
All tables are created using Stata or Matlab files
To replicate all tables, run the following files

3. To generate Table 1: 
   Run Stata file Table1_regressions.do in .../do_files/ 
   This creates Table 1, column 1 and column2 

4. To generate Table 2: 
    1) Run estimation_short_restat.m with 'thresh' in line 12 evaluated as '40' in folder .../matlab_code/.
       This file generates res_2004_40thresh_Restat7.m in .../data_matlab/.
       (This step can be skipped.)
    2) Run estimation.m in folder .../matlab_code/, and change 'thresh' value in line 10 into '40'. 
         a. Line 265--301 report values in the 1st row in Table 2.
         b. Line 385--398 report values in the 2nd row in Table 2.
         c. Line 444--470 report values in the 3rd and 4th row in Table 2.
         d. Line 146--153 report values in the 5th and 6th row in Table 2.

5. To generate Table 3:
    1) Keep 'thresh' value in line 12 as '40' in estimation.m
        a. Line 226--255 generates values in the 1st--4th row, the 1st column, in Table 3.
    2) Run estimation_short_restat.m with 'thresh' in line 12 evaluated as '20' in folder .../matlab_code/.
       This file generates res_2004_20thresh_Restat7.m in .../data_matlab/.
       (This step can be skipped.)     
    3) Run estimation.m in folder .../matlab_code/, and change 'thresh' value in line 12 into '20'. 
        a. Line 265--301 generates values in the 1st--4th row, the 2nd column, in Table 3.
        b. Line 143--153 generates values in the 5th--7th row, the 2nd column, in Table 3.

6. To generate Table 4:
   Run monte_carlo_measurement_error.m in .../matlab_code/.
    a. Comment line 201 for 'prtilde = prtilde.*exp(err1)'.
    b. Line 308--329 reports values in Table 4.

7. To generate Table 5:
   a. Uncomment line 201 to activate the command of 'prtilde = prtilde.*exp(err1)'.
   b. Line 333--348 reports values in Table 5

8. To generate Table 6:
   Run welfare_trade_barriers.m in .../matlab_code/.
    a. Change parameters as 't = .13' in line 17, 'ttar = 0' in line 19, 'tau = 0' in line 27 and 'tautar = 0' in line 31.
       This reports values for Baseline case, in the 1st row in Table 6.
    b. Change the parameters back to the baseline case as described in step (a). 
       Uncomment line 20 for 'ttar = .047', and comment line 19.
       This reports values for (A) Counterfactual case, in the 2nd row in Table 6.
    c. Change the parameters back to the baseline case as described in step (a). 
       Uncomment line 32 for 'ttar = .054496', and comment line 31.
       This reports values for (M) Counterfactual case, in the 3rh row in Table 6.
    d. Change the parameters back to the baseline case as described in step (a). 
       Uncomment line 16 for 't = 0', and comment line 17. 
       Uncomment line 28 for 'tau = .3326', and comment line 27.
       This reports values for alternative baseline case, in the 4th row in Table 6. 
	


**********************************************
***Creates Figures***
**********************************************

To create the figures of the text run the following Matlab files
 

9. Run estimation.m in .../matlab_code/.
 	a. Line 354--383 plots Figure 2.
 	b. Line 473--512 plots Figure 3.
	c. Line 322--344 plots Figure 4.
	d. Line 402--419 plots Figure 5.

